<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleConfirmPdm"
          v-hasPermi="['repertory:PdmFirmOffer:confirm']"
        >
          确认
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="single"
          @click="confirm_back = true"
          v-hasPermi="['repertory:PdmFirmOffer:back']"
        >
          撤回
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-download"
          size="mini"
          @click="exportTotal"
          v-hasPermi="['repertory:PdmMstr:export']"
        >
          待实盘数据excel导出
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-upload
          action="#"
          :show-file-list="false"
          :before-upload="beforeUpload"
          :disabled="single"
        >
          <el-button
            type="warning"
            :disabled="single"
            plain
            icon="el-icon-download"
            size="mini"
            v-hasPermi="['repertory:PdmMstr:remove']"
          >
            实盘数据Excle导入
          </el-button>
        </el-upload>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport('#exportPdm', '库存盘点数据查询-汇总')"
          v-hasPermi="['repertory:PdmMstr:export']"
        >
          Excel
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="exportTotal"
          v-hasPermi="['repertory:PdmMstr:export']"
        >
          Excel子项
        </el-button>
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      @scrollBottom="load"
      highlight-current-row
      border
      stripe
      v-loading="loading"
      :height="tableH"
      id="exportPdm"
      :cell-style="iCellStyle"
      :data="PdmMstrList"
      @row-click="clickRow"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column label="#" type="index" width="40" align="center" />
      <el-table-column
        label="盘点单号"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="pdmDjbh"
      />
      <el-table-column
        label="盘点日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="pdmPdrq"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.pdmPdrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="盘点客户"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="pdmKhdm"
      >
        <template #default="scope">
          <dict-tag :options="allCustomerOptions" :value="scope.row.pdmKhdm" />
        </template>
      </el-table-column>
      <el-table-column
        label="调整仓库"
        :show-overflow-tooltip="true"
        align="center"
        prop="pdmCkdm"
      >
        <template #default="scope">
          <dict-tag :options="allStorageOptions" :value="scope.row.pdmCkdm" />
        </template>
      </el-table-column>
      <el-table-column
        label="备注"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="pdmDesc"
      />
      <el-table-column
        label="撤回原因"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="pdmNgdesc"
      />
      <el-table-column
        label="开单人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="pdmJlry"
      />
      <el-table-column
        label="开单日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="pdmJlrq"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.pdmJlrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="确认人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="pdmQrry"
      />
      <el-table-column
        label="确认日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="pdmQrrq"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.pdmQrrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="批准人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="pdmPzry"
      />
      <el-table-column
        label="批准日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="pdmPzrq"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.pdmPzrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="过账人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="pdmGzry"
      />
      <el-table-column
        label="过账日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="pdmGzrq"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.pdmGzrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        fixed="right"
        id="pddOperate"
        align="center"
        class="pddOperate"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleAddChild(scope.row)"
            :disabled="scope.row.pdmZtbz != '1' || scope.row.pdmTjflag != '1'"
            v-hasPermi="['output:picking:removeCld']"
          >
            添加
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <u-table
      border
      stripe
      use-virtual
      :row-height="24"
      :height="tableHChild"
      :data="pddDetList"
      :cell-style="iCellStyle"
      :header-cell-style="{ 'text-align': 'center' }"
      id="exportPdd"
    >
      <el-table-column label="#" type="index" width="40" align="center" />
      <el-table-column label="id" width="1" align="center" prop="pddId" />
      <el-table-column
        label="客户编号"
        width="135"
        align="center"
        prop="pddCustWldm"
      />
      <el-table-column label="规格描述" min-width="335" prop="pddPmgg" />
      <el-table-column
        label="物料代码"
        width="135"
        align="center"
        prop="pddWldm"
      />
      <el-table-column
        label="单位"
        :show-overflow-tooltip="true"
        width="60"
        align="center"
        prop="pddDw"
      >
        <template #default="scope">
          <dict-tag :options="unitFlagOptions" :value="scope.row.pddDw" />
        </template>
      </el-table-column>
      <el-table-column
        label="盘点人员"
        width="80"
        align="center"
        prop="pddPdry"
      />
      <el-table-column
        label="盘点日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="pddPdrq"
        width="150"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.pddPdrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="库存数量"
        width="80"
        align="center"
        prop="pddKcsl"
      />
      <el-table-column
        label="实盘数量"
        width="80"
        align="center"
        prop="pddPdsl"
      />
      <el-table-column
        label="盘点差异"
        width="80"
        align="center"
        prop="pddCysl"
      />
      <el-table-column align="center">
        <template #header #default="scope">
          <div class="export-hide">操作</div>
        </template>
        <template #default="scope">
          <div style="display: flex; text-align: center">
            <el-button
              :disabled="single"
              @click="handleFirmOffer(scope.row)"
              type="primary"
              size="mini"
              plain
              icon="el-icon-check"
              >实盘</el-button
            >
            <el-button
              :disabled="single"
              @click="handlePdd(scope.row)"
              type="primary"
              size="mini"
              >一键实盘</el-button
            >
            <el-button
              :disabled="single || scope.row.pddFlag != 1"
              @click="handleDeletePdd(scope.row)"
              size="mini"
              type="danger"
              icon="el-icon-delete"
            >
              删除
            </el-button>
          </div>
        </template>
      </el-table-column>
    </u-table>

    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="650px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-col :span="24">
          <el-form-item label="开单日期">
            <el-date-picker
              v-model="dateRange"
              size="small"
              style="width: 240px"
              value-format="yyyy-MM-dd HH:mm:ss"
              :default-time="['00:00:00', '23:59:59']"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="盘点单号" prop="pdmDjbh">
            <el-input
              v-model="queryParams.pdmDjbh"
              placeholder="请输入盘点单号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="盘点仓库" prop="pdmCkdm">
            <el-select
              v-model="queryParams.pdmCkdm"
              filterable
              clearable
              placeholder="请选择盘点仓库"
            >
              <el-option
                v-for="item in storageOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item>
            <RadioGroup v-model="queryParams.pdmZtbz">
              <Radio label="1" style="margin: 0px 30px">未确认</Radio>
              <Radio label="2" style="margin-right: 30px">已确认</Radio>
              <Radio label="" align="right">全部</Radio>
            </RadioGroup>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>

    <!-- 盘点对话框   -->
    <el-dialog
      :title="title"
      v-model="firmOpen"
      :close-on-click-modal="false"
      draggable
      width="680px"
      append-to-body
    >
      <el-form ref="form" :model="form" label-width="88px" :rules="rules">
        <el-col :span="12">
          <el-form-item label="盘点单号" prop="pddDjbh">
            <el-input v-model="form.pddDjbh" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="存放位置" prop="pddCwdm">
            <el-select v-model="form.pddCwdm" disabled>
              <el-option
                v-for="item in allLocationOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="品名规格" prop="pddPmgg">
            <el-input v-model="form.pddPmgg" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="物料代码" prop="pddWldm">
            <el-input v-model="form.pddWldm" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="库存数量" prop="pddKcsl">
            <el-input v-model="form.pddKcsl" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="实盘数量" prop="pddPdsl">
            <el-input type="number" v-model="form.pddPdsl" />
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleFirmSubmit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 撤回 -->
    <Modal v-model="confirm_back" draggable scrollable width="300">
      <p slot="header" style="color: #f60; text-align: center">
        <Icon type="ios-information-circle"></Icon>
        <span>撤回</span>
      </p>
      <div style="text-align: center">
        <p>是否将【盘点单号：{{ clickData.pdmDjbh }}】撤回？</p>
        <Input style="width: 65%" placeholder="撤回理由" v-model="backReason" />
      </div>
      <div slot="footer">
        <Button type="error" size="large" @click="handleBack" long>撤回</Button>
      </div>
    </Modal>

    <!-- 添加对话框 -->
    <el-dialog
      v-model="childOpen"
      customClass="customWidth"
      :fullscreen="true"
      :close-on-click-modal="false"
      append-to-body
    >
      <!-- 添加物料搜索条件框-->
      <el-form :model="queryChildParams" :inline="true">
        <el-form-item label="品名规格" prop="pmgg">
          <el-input
            v-model="queryChildParams.params.pmgg"
            placeholder="请输入品名规格"
            clearable
            size="small"
            @keyup.enter.native="searchChildren"
          />
        </el-form-item>
        <el-form-item style="justify-content: flex-end">
          <el-button type="primary" @click="searchChildren">查询</el-button>
        </el-form-item>
      </el-form>
      <!--待接收物料-->
      <el-form>
        <el-table
          highlight-current-row
          v-loading="loading"
          :data="searchList"
          border
          stripe
          ref="checkTable"
          :height="tableHAdd"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column label="#" type="index" align="center" fixed="left" />
          <el-table-column
            label="单据编号"
            :show-overflow-tooltip="true"
            align="center"
            prop="pddDjbh"
          />
          <el-table-column
            label="客户编码"
            :show-overflow-tooltip="true"
            align="center"
            prop="pdd_cuswldm"
          />
          <el-table-column
            label="客户物料代码"
            :show-overflow-tooltip="true"
            align="center"
            prop="pddWldm"
          />
          <el-table-column
            label="品名规格"
            :show-overflow-tooltip="true"
            align="center"
            prop="pdd_pmgg"
          />
          <el-table-column
            label="客户简称"
            :show-overflow-tooltip="true"
            align="center"
            prop="pdd_khdm"
          />
          <el-table-column
            label="仓库位置"
            :show-overflow-tooltip="true"
            align="center"
            prop="pdd_ckdm"
          >
            <template #default="scope">
              <dict-tag
                :options="allStorageOptions"
                :value="scope.row.pdd_ckdm"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="存放位置"
            :show-overflow-tooltip="true"
            align="center"
            prop="pddCwdm"
          >
            <template #default="scope">
              <dict-tag
                :options="allLocationOptions"
                :value="scope.row.pddCwdm"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="库存数量"
            :show-overflow-tooltip="true"
            align="center"
            prop="pddKcsl"
          />
        </el-table>
        <!-- 底部确定取消 -->
        <el-form-item
          style="margin-top: 25px; display: flex; justify-content: flex-end"
        >
          <el-button @click="cancel">取消</el-button>
          <el-button type="primary" @click="handAddChildren">确认</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPdmMstr,
  listPddDet,
  submitPdmMstr,
  searchChildData,
  savePdd,
  getPddById,
  firmSubmit,
  importPdd,
  confirmPdm,
  backConfirm,
  delPddDet,
  exportFirm,
} from "@/api/repertory/PdmMstr";
import { getAll, getstorageType } from "@/api/repertory/Storage";
import { getAllCustomer, getAllCustomerTwo } from "@/api/project/customer";
import { exportTable } from "@/utils/ruoyi";

export default {
  name: "PdmMstr",
  data() {
    return {
      /** table 高度 **/
      tableH: (document.body.clientHeight - 130) / 2,
      tableHChild: (document.body.clientHeight - 280) / 2,
      tableHAdd: document.body.clientHeight - 320,
      /** 颜色事件 */
      iCellStyle: function ({ row, column, rowIndex, columnIndex }) {
        //提交
        if (row.pdmZtbz == "1" && row.pdmTjflag == "1") {
          return "padding:0px 0;color:#0fac09";
        }
        //确认
        else if (row.pdmZtbz == "2" && row.pdmTjflag == "1") {
          return "padding:0px 0;color:#0323af";
        }
        //批准
        else if (row.pdmZtbz == "4" && row.pdmTjflag == "1") {
          return "padding:0px 0;color:#a503bb";
        }
        //过账
        if (row.rwmZtbz == "6" && row.pdmTjflag == "1") {
          return "padding:0px 0;color:#bb0331";
        }
        //子表
        if (row.pddPdsl >= 0) {
          return "padding:0px 0;color:#bb0331";
        }
      },
      /** 遮罩层和弹窗**/
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      // 添加子项弹窗
      childOpen: false,
      // 实盘弹窗
      firmOpen: false,
      // 撤回弹窗
      confirm_back: false,
      exportLoading: false,
      // 弹出层标题
      title: "",
      //下载内容
      upDateContainer: null,
      // 撤回原因
      backReason: null,

      /** 选中数据和List */
      // 头表单击选中数组
      clickData: {},
      // 库存盘点头表数据
      PdmMstrList: [],
      // 库存盘点表格总条数
      total: 0,
      // 库存盘点详细表数据
      pddDetList: [],
      // "添加"表格数据
      searchList: [],
      // "添加"表格总条数
      totalChild: 0,
      // "添加" 选中的列表
      selectioned: [],

      /** 查询参数及初始化*/
      // 开单日期时间范围
      dateRange: [
        this.$moment().add(-1, "month").startOf("month").format(),
        this.$moment().add(2, "month").endOf("month").format(),
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 25,
        pdmDjbh: null,
        pdmCkdm: null,
        pdmTjflag: 1,
        pdmZtbz: "1",
        pdmJlrq: null,
      },
      // 添加按钮查询参数
      queryChildParams: {
        pageNum: 1,
        pageSize: 25,
        pdmDjbh: null,
        pdmCkdm: null,
        pdmKhdm: null,
        params: {
          pmgg: null,
        },
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        pdmCkdm: [
          { required: true, message: "盘底仓库不能为空", trigger: "blur" },
        ],
        pdmPdrq: [
          { required: true, message: "盘底日期不能为空", trigger: "blur" },
        ],
      },
      /** 字典数据 **/
      //有效仓库字典
      storageOptions: [],
      //所有仓库字典
      allStorageOptions: [],
      //客户类型
      customerOptions: [],
      //所有客户类型
      allCustomerOptions: [],
      //单位
      unitFlagOptions: [],
      //所有储位信息
      allLocationOptions: [],
    };
  },
  created() {
    //有效仓库
    getstorageType("0").then((response) => {
      this.storageOptions = response.data;
      for (let item of this.storageOptions) {
        item.dictLabel = item.storageName;
        item.listClass = "default";
        item.dictValue = item.storageId + "";
      }
    });
    //单位
    this.getAllDicts("sys_unit").then((response) => {
      this.unitFlagOptions = response.data;
    });
    //所有仓库
    getAll().then((response) => {
      this.allStorageOptions = response.data;
      for (let item of this.allStorageOptions) {
        item.dictLabel = item.storageName;
        item.listClass = "default";
        item.dictValue = item.storageId + "";
      }
    });
    //所有储位
    getAll().then((response) => {
      this.allLocationOptions = response.data;
      for (let item of this.allLocationOptions) {
        item.dictLabel = item.storageDefaultstorage;
        item.listClass = "default";
        item.dictValue = item.storageId + "";
      }
    });

    getAllCustomer().then((response) => {
      for (let i of response.data) {
        i.dictLabel = i.cusName;
        i.dictValue = i.cusId + "";
        i.listClass = "default";
      }

      this.customerOptions = response.data;
    });

    getAllCustomerTwo().then((response) => {
      for (let i of response.data) {
        i.dictLabel = i.cusName;
        i.dictValue = i.cusId + "";
        i.listClass = "default";
      }

      this.allCustomerOptions = response.data;
    });

    this.getList();
  },
  methods: {
    /** 查询库存盘点列表 */
    getList(row) {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.dateRange && "" != this.dateRange) {
        this.queryParams.params["beginPdmJlrq"] = this.moment(
          this.dateRange[0]
        ).format("YYYY-MM-DD HH:mm:ss");
        this.queryParams.params["endPdmJlrq"] = this.moment(
          this.dateRange[1]
        ).format("YYYY-MM-DD HH:mm:ss");
      }
      this.queryParams.pageNum = 1;
      listPdmMstr(this.queryParams).then((response) => {
        this.PdmMstrList = [];
        this.pddDetList = [];
        this.PdmMstrList = this.PdmMstrList.concat(response.rows);
        this.total = response.total;
        this.loading = false;
        if (row != null) {
          this.clickRow(row);
        } else if (this.PdmMstrList.length > 0) {
          this.clickRow(this.PdmMstrList[0]);
        }
      });
    },

    //load
    load() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total / this.queryParams.pageSize)
      ) {
        this.queryParams.pageSize = 25;
        this.queryParams.pageNum++;
        listPdmMstr(this.queryParams).then((response) => {
          this.PdmMstrList = this.PdmMstrList.concat(response.rows);
          this.total = response.total;
          this.loading = false;
        });
      }
      return 0;
    },
    // 取消按钮
    cancel() {
      this.firmOpen = false;
      this.childOpen = false;
      this.open = false;
      this.openSearch = false;
      this.backReason = null;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        pddId: null,
        pddDjbh: null,
        pddWldm: null,
        pddPmgg: null,
        pddDw: null,
        pddKhdm: null,
        pddCwdm: null,
        pddKcsl: null,
        pddPdsl: null,
        pddCysl: null,
        pddFlag: null,
        pddPdry: null,
        pddPdrq: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.PdmMstrList = [];
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /**  搜索数据 */
    handleSearch() {
      this.reset();
      this.title = "查询库存盘点";
      this.openSearch = true;
    },
    /** 多选框选中数据 */
    clickRow(row) {
      this.clickData = row;
      this.single = row == null || row.pdmZtbz != "1" || row.pdmTjflag != 1;
      //获得选中的详细数据
      this.getPddDetList(row.pdmDjbh);
    },
    /** 获取明细列表 */
    getPddDetList(pdmDjbh) {
      this.pddDetList = [];
      listPddDet(pdmDjbh).then((response) => {
        this.pddDetList = response.data;
      });
    },
    /** 添加-子项按钮操作 */
    handleAddChild(row) {
      this.queryChildParams.pdmDjbh = row.pdmDjbh;
      this.queryChildParams.pdmKhdm = row.pdmKhdm;
      this.queryChildParams.pdmCkdm = row.pdmCkdm;
      this.searchChildren();
    },
    /** 添加-搜索数据 */
    searchChildren() {
      this.searchList = [];
      searchChildData(this.queryChildParams).then((res) => {
        this.searchList = res.rows;
        this.totalChild = res.total;
        this.childOpen = true;
      });
    },
    /** 添加-保存数据 */
    handAddChildren() {
      const datas = this.searchList;
      // const datas = this.searchList.filter((a) => {
      //   return !!a.pddKcsl
      // })
      if (datas.length < 1) {
        this.msgError("请输入需要添加的数据");
        return;
      }
      savePdd(datas).then((res) => {
        if (res.code == "200") {
          this.searchChildren();
          this.getPddDetList(this.clickData.pdmDjbh);
          this.msgSuccess("添加成功~");
        }
      });
    },
    /** 实盘按钮操作 */
    handleFirmOffer(row) {
      getPddById(row.pddId).then((res) => {
        this.reset();
        this.form = res.data;
        this.form.pddPdsl = this.form.pddKcsl;
        this.firmOpen = true;
      });
    },
    handlePdd(row) {
      getPddById(row.pddId).then((res) => {
        this.reset();
        this.form = res.data;
        this.form.pddPdsl = this.form.pddKcsl;
        this.handleFirmSubmit();
      });
    },
    /** 实盘-提交 */
    handleFirmSubmit() {
      firmSubmit(this.form).then((res) => {
        if (res.code == "200") {
          this.getPddDetList(this.form.pddDjbh);
          this.firmOpen = false;
          this.msgSuccess("实盘成功");
        }
      });
    },
    /** 实盘数据Excle导入 预处理 */
    beforeUpload(file) {
      const reader = new FileReader();
      reader.readAsDataURL(file);
      reader.onload = () => {
        this.upDateContainer = reader.result;
        let formData = new FormData();

        formData.append("pdmDjbh", this.clickData.pdmDjbh);
        formData.append("file", file);
        // console.log(this.clickData.pdmDjbh)
        importPdd(formData).then((response) => {
          this.getPddDetList(this.clickData.pdmDjbh);
          this.$message.success("批量导入成功");
        });
      };
    },

    /** 确认按钮 */
    handleConfirmPdm() {
      const pdmId = this.clickData.pdmId;
      if (this.isEmpty(this.pddDetList)) {
        alert("存在未进行实盘数量确认的物料，不允许此操作");
        return;
      } else {
        this.$confirm(
          "是否将【盘点单号：" + this.clickData.pdmDjbh + "】确认？",
          "提示",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }
        )
          .then(function () {
            return confirmPdm(pdmId);
          })
          .then(() => {
            this.getList();
            this.msgSuccess("确认成功");
          })
          .catch(() => {});
      }
    },

    /** 效验盘点数据是否为空 */
    isEmpty(data) {
      for (const i of data) {
        if (i.pddPdsl == null || i.pddPdsl < 0) return true;
      }
      return false;
    },
    /** 删除子项明细 */
    handleDeletePdd(row) {
      delPddDet(row.pddId).then((res) => {
        this.msgSuccess("删除成功");
        this.getPddDetList(row.pddDjbh);
      });
    },
    /** 提交按钮操作 */
    handleSubmit() {
      submitPdmMstr(this.clickData.pdmId).then((res) => {
        if (res.code == "200") {
          this.getList();
          this.msgSuccess("提交成功");
        }
      });
    },
    /** 实盘确认-撤回 */
    handleBack() {
      if (this.backReason == null || this.backReason == "") {
        this.msgError("撤回理由必填");
        return;
      }
      backConfirm(this.clickData.pdmId, this.backReason).then((res) => {
        this.confirm_back = false;
        this.msgSuccess("撤回成功");
      });
    },
    handleExport(a, b) {
      exportTable(a, b);
    },
    exportTotal() {
      const order = this.clickData.pdmDjbh;
      this.$confirm("是否确认导出?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportFirm({ pdmDjbh: order });
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        });
    },
  },
};
</script>

<style lang="scss" scoped></style>
